//
//  CZQTabBar.m
//  weibo
//
//  Created by Apple on 15-1-30.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZQTabBar.h"
#import "CZQTabBarButton.h"

@interface CZQTabBar()
@property (nonatomic,weak)UIButton *plusButton;
//当前按钮zhuangt
@property(nonatomic,weak)CZQTabBarButton *currentBtn;

@property(nonatomic,strong)NSMutableArray *btns;

@end
@implementation CZQTabBar
/**
 *  懒加载
 */
- (NSMutableArray *)btns{
    if (_btns==nil) {
        _btns = [NSMutableArray array];
    }
    return _btns;

}
- (id)initWithCoder:(NSCoder *)aDecoder{

    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return  self;

}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       [self setup];
    }
    return self;
}
//初始化的方法
- (void)setup{

   self.backgroundColor = [UIColor whiteColor];
    
    //增加+号按钮
    
    [self addPlusBtn];

}
//增加+号按钮
- (void)addPlusBtn{
    // 1.创建按钮
    UIButton *plusBtn = [[UIButton alloc] init];
    plusBtn.tag = 999;
    // 2.设置背景图片
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    // 3.设置图标
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    self.plusButton = plusBtn;
    
    [self  addSubview:plusBtn];


}
- (void)addItem:(UITabBarItem *)item{
    //1.创建选项卡按钮
    CZQTabBarButton *tabBarBtn = [[CZQTabBarButton alloc] init];
    
    tabBarBtn.item = item;
    
    [self addSubview:tabBarBtn];
    
    [tabBarBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    //设置tag
    tabBarBtn.tag =self.btns.count;
    
    [self.btns addObject:tabBarBtn];

    
}
- (void)btnClick:(CZQTabBarButton *)btn{
    
    if ([self.delegate respondsToSelector:@selector(tabBar:from:to:)]) {
        [self.delegate tabBar:self from:self.currentBtn.tag to:btn.tag];
    }

    self.currentBtn.selected = NO;
    
    btn.selected = YES;
    
    self.currentBtn = btn;
    // 3.让当前选中按钮执行动画
    // 缩小 -- > 放大 --> 还原
    [UIView animateWithDuration:0.2 animations:^{
        // 缩小
        // sx / sy 如果是1代表不缩放, 如果大于1代表放大 如果小于1代表缩小
        btn.transform = CGAffineTransformMakeScale(0.4, 0.4);
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.1 animations:^{
            // 放大
            btn.transform = CGAffineTransformMakeScale(1.4, 1.4);
            
        } completion:^(BOOL finished) {
            
            [UIView animateWithDuration:0.1 animations:^{
                // 还原
                btn.transform = CGAffineTransformIdentity;
            }];
        }];
    }];



}
- (void)layoutSubviews{

    [super layoutSubviews];

     NSUInteger index = 0;
 
    for (UIView *child in self.subviews) {
  //tag不为999的进行遍历  同时设置frame  tag为999的则则直接遍历后设置frame；
        if (child.tag != 999) {
            // 2.计算每个按钮的frame
            CGFloat childW = self.frame.size.width / 5;
            CGFloat childH = self.frame.size.height;
            CGFloat childX = index * childW;
            CGFloat childY = 0;
            
            // 判断是否是中间一个按钮
            if (index == 2) {
                index++;
                childX = index * childW;
            }
            index++;
            child.frame = CGRectMake(childX, childY, childW, childH);
            
        }
      
        // 设置加号按钮的frame
        self.plusButton.size = self.plusButton.currentBackgroundImage.size;
        self.plusButton.centerX = self.width * 0.5;
        self.plusButton.centerY = self.height * 0.5;
    }

     // NSLog(@"%@----",self.subviews );
}
@end
